<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs2.model.*, bbs2.dao.*, bbs2.service.*, bbs2.util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="../_checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="../_header.jsp" %>
<style>
    div.header span:nth-child(1) { color: #aaa; }
    div.header span:nth-child(3) { color: #aaa; margin-left: 20px; }
    div.header span:nth-child(5) { margin-left: 20px; }
    h2 small { font-size: 10pt; }
    div.body { min-height: 400px; }
    iframe { width: 100%;  border: 1px none; }
</style>
</head>
<script>
function resize_iframe() {
    $('iframe').css("height", $('iframe').contents().find("html").height());
}
</script>
<%
    RequestParameter param = new RequestParameter(request);
    int bid = param.getInt("bid", 0);
    int aid = param.getInt("aid", 0);
    
    int userId = UserService.getCurrentUser(session).getId();
    BoardAccessRule rule = BoardAccessRuleDAO.selectByUserId(bid, userId);
    if (!rule.canReadArticle()) {
        response.sendRedirect("/bbs2/guest/error403.jsp");
        return;
    }
   
    Board board = BoardDAO.selectById(bid);
    Article article = ArticleDAO.selectById(aid, UserService.getCurrentUser(session).getId());

    String urlList = "list.jsp?" + request.getQueryString().replaceAll("&?aid=[0-9]+&?","");
    String urlEdit = "edit.jsp?" + request.getQueryString();
    String urlDelete = "article_action.jsp?cmd=delete&" + request.getQueryString();
    String urlNotice = "article_action.jsp?cmd=notice&" + request.getQueryString();
    String urlReplay = "create.jsp?bid=" + bid + "&aid=" + aid;
%>
<body>
<%@ include file="../_menu.jsp" %>  

<div class="container">

    <div class="controls pull-right">
        <a href="<%= urlList %>" class="btn btn-small">목록으로</a>
        <% if (rule.canManageBoard()) { %>
            <a class="btn btn-small" href="<%= urlNotice %>">공지글 <%= article.isNotice() ? "취소" : "설정" %></a>
        <% } %>
        <% if (rule.canEditArticle() || (rule.canEditMyArticle() && article.getUserId() == userId) ) { %>
            <a class="btn btn-small" href="<%= urlEdit %>">수정</a>
        <% } %>
        <% if (rule.canDeleteArticle() || (rule.canDeleteMyArticle() && article.getUserId() == userId) ) { %>
            <a class="btn btn-small" href="<%= urlDelete %>" onclick="return confirm('삭제하시겠습니까?');">삭제</a>
        <% } %>
        <% if (rule.canCreateArticle()) { %>
            <a class="btn btn-small" href="<%= urlReplay %>">댓글쓰기</a>
        <% } %>
    </div>

    <h1><%= board.getTitle() %></h1>
    <hr />    

    <h2><small>[<%= article.getCategory() %>]</small> <%= article.getTitle() %></h2>
    <hr />

    <div class="header">
        <span>작성자:</span>
        <span><%= article.getName() %></span>
        <span>작성일:</span>
        <span><%= DateUtil.toYMDHMS(article.getWriteTime()) %></span>
        <% if (article.isNotice()) { %>
            <span class="label label-important">공지</span>        
        <% } %>
    </div>
    <hr />

     <div class="body">
        <%= article.getBody() %>
    </div>

    <% if (board.isFileAttachEnabled()) { %> 
        <iframe class="file" src="/bbs2/file/download_list.jsp?targetType=1&targetId=<%= aid %>">
        </iframe>
    <% } %>
    
    <% if (board.isCommentEnabled()) { %> 
        <hr />
        <iframe class="comment" src="/bbs2/comment/list.jsp?targetType=1&targetId=<%= aid %>">
        </iframe>
    <% } %>
    
</div>

<%@ include file="../_footer.jsp" %>  
</body>
</html>
